<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Estimating Sample Sizes for a Binomial Distribution.</title>
<link rel="stylesheet" href="../../../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Math Toolkit 3.3.0">
<link rel="up" href="../binom_eg.html" title="Binomial Distribution Examples">
<link rel="prev" href="binom_conf.html" title="Calculating Confidence Limits on the Frequency of Occurrence for a Binomial Distribution">
<link rel="next" href="../geometric_eg.html" title="Geometric Distribution Examples">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="binom_conf.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../binom_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../geometric_eg.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="math_toolkit.stat_tut.weg.binom_eg.binom_size_eg"></a><a class="link" href="binom_size_eg.html" title="Estimating Sample Sizes for a Binomial Distribution.">Estimating
          Sample Sizes for a Binomial Distribution.</a>
</h5></div></div></div>
<p>
            Imagine you have a critical component that you know will fail in 1 in
            N "uses" (for some suitable definition of "use").
            You may want to schedule routine replacement of the component so that
            its chance of failure between routine replacements is less than P%. If
            the failures follow a binomial distribution (each time the component
            is "used" it either fails or does not) then the static member
            function <code class="computeroutput"><span class="identifier">binomial_distibution</span><span class="special">&lt;&gt;::</span><span class="identifier">find_maximum_number_of_trials</span></code>
            can be used to estimate the maximum number of "uses" of that
            component for some acceptable risk level <span class="emphasis"><em>alpha</em></span>.
          </p>
<p>
            The example program <a href="../../../../../../example/binomial_sample_sizes.cpp" target="_top">binomial_sample_sizes.cpp</a>
            demonstrates its usage. It centres on a routine that prints out a table
            of maximum sample sizes for various probability thresholds:
          </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">find_max_sample_size</span><span class="special">(</span>
   <span class="keyword">double</span> <span class="identifier">p</span><span class="special">,</span>              <span class="comment">// success ratio.</span>
   <span class="keyword">unsigned</span> <span class="identifier">successes</span><span class="special">)</span>    <span class="comment">// Total number of observed successes permitted.</span>
<span class="special">{</span>
</pre>
<p>
            The routine then declares a table of probability thresholds: these are
            the maximum acceptable probability that <span class="emphasis"><em>successes</em></span>
            or fewer events will be observed. In our example, <span class="emphasis"><em>successes</em></span>
            will be always zero, since we want no component failures, but in other
            situations non-zero values may well make sense.
          </p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">alpha</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> <span class="number">0.5</span><span class="special">,</span> <span class="number">0.25</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.05</span><span class="special">,</span> <span class="number">0.01</span><span class="special">,</span> <span class="number">0.001</span><span class="special">,</span> <span class="number">0.0001</span><span class="special">,</span> <span class="number">0.00001</span> <span class="special">};</span>
</pre>
<p>
            Much of the rest of the program is pretty-printing, the important part
            is in the calculation of maximum number of permitted trials for each
            value of alpha:
          </p>
<pre class="programlisting"><span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">alpha</span><span class="special">)/</span><span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">alpha</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
<span class="special">{</span>
   <span class="comment">// Confidence value:</span>
   <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">fixed</span> <span class="special">&lt;&lt;</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">10</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">right</span> <span class="special">&lt;&lt;</span> <span class="number">100</span> <span class="special">*</span> <span class="special">(</span><span class="number">1</span><span class="special">-</span><span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">]);</span>
   <span class="comment">// calculate trials:</span>
   <span class="keyword">double</span> <span class="identifier">t</span> <span class="special">=</span> <span class="identifier">binomial</span><span class="special">::</span><span class="identifier">find_maximum_number_of_trials</span><span class="special">(</span>
                  <span class="identifier">successes</span><span class="special">,</span> <span class="identifier">p</span><span class="special">,</span> <span class="identifier">alpha</span><span class="special">[</span><span class="identifier">i</span><span class="special">]);</span>
   <span class="identifier">t</span> <span class="special">=</span> <span class="identifier">floor</span><span class="special">(</span><span class="identifier">t</span><span class="special">);</span>
   <span class="comment">// Print Trials:</span>
   <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">fixed</span> <span class="special">&lt;&lt;</span> <span class="identifier">setprecision</span><span class="special">(</span><span class="number">5</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">15</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">right</span> <span class="special">&lt;&lt;</span> <span class="identifier">t</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            Note that since we're calculating the maximum number of trials permitted,
            we'll err on the safe side and take the floor of the result. Had we been
            calculating the <span class="emphasis"><em>minimum</em></span> number of trials required
            to observe a certain number of <span class="emphasis"><em>successes</em></span> using
            <code class="computeroutput"><span class="identifier">find_minimum_number_of_trials</span></code>
            we would have taken the ceiling instead.
          </p>
<p>
            We'll finish off by looking at some sample output, firstly for a 1 in
            1000 chance of component failure with each use:
          </p>
<pre class="programlisting">________________________
Maximum Number of Trials
________________________

Success ratio                           =  0.001
Maximum Number of "successes" permitted =  0


____________________________
Confidence        Max Number
 Value (%)        Of Trials
____________________________
    50.000            692
    75.000            287
    90.000            105
    95.000             51
    99.000             10
    99.900              0
    99.990              0
    99.999              0
</pre>
<p>
            So 51 "uses" of the component would yield a 95% chance that
            no component failures would be observed.
          </p>
<p>
            Compare that with a 1 in 1 million chance of component failure:
          </p>
<pre class="programlisting">________________________
Maximum Number of Trials
________________________

Success ratio                           =  0.0000010
Maximum Number of "successes" permitted =  0


____________________________
Confidence        Max Number
 Value (%)        Of Trials
____________________________
    50.000         693146
    75.000         287681
    90.000         105360
    95.000          51293
    99.000          10050
    99.900           1000
    99.990            100
    99.999             10
</pre>
<p>
            In this case, even 1000 uses of the component would still yield a less
            than 1 in 1000 chance of observing a component failure (i.e. a 99.9%
            chance of no failure).
          </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="binom_conf.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../binom_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../geometric_eg.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
